TOOL.Category = "Shield Systems"
TOOL.Name = "#Contraption Shield"
TOOL.Command = nil
TOOL.ConfigName = ""
TOOL.Tab = "Contraption Engine"

TOOL.ClientConVar["material"] = "models/props_combine/com_shield001a"
TOOL.ClientConVar["HP"] = "1000"
TOOL.ClientConVar["AutoWeld"] = "0"
TOOL.ClientConVar["model"] = "models//hunter//misc//sphere2x2.mdl"
TOOL.EntityClass = "Shield_Bubble_Generator"

if (SERVER) then
	CreateConVar('sbox_maxShieldConform', 4)
end

if ( CLIENT ) then
	language.Add( "Tool_shield_conform_name", "Contraption Shield Generator" )
	language.Add( "Tool_shield_conform_desc", "Projects a hull-conforming shield that absorbs weapons damage" )
	language.Add( "Tool_shield_conform_0", "Left-Click to Spawn a Shield, Right-Click to Set Model" )
	
	language.Add( "Undone_shield_conform", "Undone contraption shield" )
	
	language.Add( "Cleanup_shield_conform", "Cleanup Contraption Shields" )
	language.Add( "Cleaned_shield_conform", "Cleaned up all Contraption Shields" )
	language.Add( "SBoxLimit_shield_conform", "Reached the Shield Limit" )
end

function TOOL:SpawnEntity(entity, position, trace)
	local newentity = ents.Create(entity)
	newentity:SetAngles( trace.HitNormal:Angle() + Angle(90,0,0))
	newentity:SetModel( self:GetClientInfo("model") )
	newentity:SetPos(position - Vector(0, 0, newentity:OBBMins().z))
	newentity.MaxHP = self:GetClientNumber("HP")
	newentity:SetPlayer(self:GetOwner())
	newentity:Spawn()
	
	if util.tobool(self:GetClientNumber("AutoWeld")) then
		local phys = newentity:GetPhysicsObject()  	
		if (phys:IsValid()) then  		
			local weld = constraint.Weld(newentity, trace.Entity, 0, trace.PhysicsBone, 0)
			local nocollide = constraint.NoCollide(newentity, trace.Entity, 0, trace.PhysicsBone)
		end 
	end
	
	self:GetOwner():AddCount( "ShieldConform", newentity ) 
	
	undo.Create("shield_conform")
		undo.AddEntity( newentity )
		undo.AddEntity( weld )
		undo.AddEntity( nocollide )
		undo.SetPlayer( self:GetOwner() )
	undo.Finish()
end

function TOOL:LeftClick( trace )
if (CLIENT) then return true end
if ( !trace.Hit ) then return end
local ply = self:GetOwner()
if (trace.Entity:GetClass() == "shield_hull_generator") then
	local shieldmod = trace.Entity
	local HP = self:GetClientNumber("HP")
	shieldmod.MaxHP = HP
	shieldmod.RegenAmount = HP / math.log10(HP) / 40
	shieldmod:GetContraption()
	return true
else

if (!ply:CheckLimit( "ShieldConform" )) then return end
	
	if ( SERVER && !util.IsValidPhysicsObject( trace.Entity, trace.PhysicsBone ) ) then return false end
	
	local SpawnPos = trace.HitPos
	self:SpawnEntity( "shield_hull_generator", SpawnPos, trace)
	return true
end
end

function TOOL:RightClick( trace )
	if CLIENT and trace.Entity:IsValid() then return true end
	if not trace.Entity:IsValid() then return end
	local model = trace.Entity:GetModel()
	self:GetOwner():ConCommand("shield_conform_model ".. model)
	return true
end

if CLIENT then

function TOOL.BuildCPanel( CPanel )
	CPanel:ClearControls()
	CPanel:AddHeader()
	CPanel:AddDefaultControls()
	// HEADER
	CPanel:AddControl( "Header", { Text = "#Tool_shield_conform_name", Description = "#Tool_shield_conform_desc" }  )
	CPanel:AddControl( "CheckBox", { Label = "Autoweld", Command = "shield_conform_AutoWeld" } )
	CPanel:AddControl( "Slider", { Label = "Shield Health", Command = "shield_conform_HP", min = 500, max = 40000 } )
end
end